<?php

namespace app\admin\controller;

use app\admin\model\Cart;
use app\admin\model\Order as ModelOrder;
use app\admin\model\order\Goods;
use app\common\controller\Backend;
use think\Db;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;
    protected $searchFields = 'order_no';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $user_id = $this->auth->id;
            $group_id = DB::name('auth_group_access')->where('uid',$user_id)->value('group_id');
            if($group_id == 7){
                $op['order.status'] = array('in',['2','3','4']);
            }
            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('user')->visible(['nickname']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function addOrder(){
        $cart_ids = input('cart_ids');
        $cart_ids = explode(',',$cart_ids);
        $list = (new Cart())::with('product')->whereIn('id',$cart_ids)->select();
        if(!$list){
            return json_encode(['code'=>1,'msg'=>'所选商品已下架']);
        }else{
            $order_no = rand(100,999).time();
            $nums = (new Cart())::with('product')->whereIn('id',$cart_ids)->sum('num');
            $user_id = $this->auth->id;
            (new ModelOrder())->save(compact('order_no','nums','user_id'));
            foreach($list as $v){
                $narr[] = [
                    'user_id'=>$this->auth->id,
                    'product_id'=>$v->product_id,
                    'nums'=>$v->num,
                    'image_urls'=>$v->product->image_urls,
                    'product_name'=>$v->product->product_name,
                    'supplier_id'=>$v->product->supplier_id,
                    'product_code'=>$v->product->product_code,
                    'product_category'=>$v->product->product_category,
                    'product_material'=>$v->product->product_material,
                    'product_technology'=>$v->product->product_technology,
                ];
            }
        }
    }
    // 订单详情
    public function details(){
        $id = input('id');
        $order = $this->model->where('id',$id)->find();
        $order_item = (new Goods())->where('order_id',$id)->select();
        foreach($order_item as $v){
            $v->image_urls = cdnurl($v->image_urls,true);
        }
        return json_encode(['code'=>1,'order'=>$order,'order_item'=>$order_item]);
    }
    public function detail($ids){
        $order = $this->model->with('user')->where('order.id',$ids)->find();
        $status_arr = ['','已提交','已审核','已发货','已完成'];
        $order->status_text = $status_arr[$order->status];
        $order_item = (new Goods())::with('supplier')->where('order_id',$ids)->select();
        $user_id = $this->auth->id;
        $group_id = DB::name('auth_group_access')->where('uid',$user_id)->column('group_id');
       
        
        foreach($order_item as $v){
            if(in_array(8,$group_id) ){
                $v->supplier->supplier_name = '****';
            }
        }
        $this->assign('order',$order);
        $this->assign('goods',$order_item);
        return $this->view->fetch();
    }

    // 确认收货
    // 1 已提交 2 已审核 3 已发送 4已完成
    // public function confim(){
    //     $id = input('id');
    //     $info = $this->model->get($id);
    //     if($info['admin_id'] != $this->auth->id){
    //         return json_encode(['code'=>0,'msg'=>"非本人操作"]);
    //     }
    //     if($info['status'] == 4){
    //         return json_encode(['code'=>0,'msg'=>"订单已确认收货"]);
    //     }
    //     if($info['status'] != 3){
    //         return json_encode(['code'=>0,'msg'=>"不可操作"]);
    //     }
    //     $info->status= 4;
    //     $info->endtime = time();
    //     return json_encode(['code'=>1,'msg'=>"操作成功"]);
    // }
    // 审核
    public function check(){
        $id = input('id');
        $info = $this->model->get($id);
        $user_id = $this->auth->id;
        $group_id = DB::name('auth_group_access')->where('uid',$user_id)->column('group_id');
       
        if(!in_array(1,$group_id) && !in_array(8,$group_id) ){
            return ['code'=>0,'msg'=>"无审核权限"];
        }
        if($info['status'] == 2){
            return ['code'=>0,'msg'=>"订单已审核"];
        }
        if($info['status'] != 1){
            return ['code'=>0,'msg'=>"不可操作"];
        }
        $info->status= 2;
        $info->check_id= $this->auth->id;
        $info->checktime = time();
        $info->save();
        return ['code'=>1,'msg'=>"操作成功"];
    }
        // 发货
        public function send(){
            $id = input('id');
            $info = $this->model->get($id);
            $user_id = $this->auth->id;
            $group_id = DB::name('auth_group_access')->where('uid',$user_id)->column('group_id');
            if(!in_array(1,$group_id) && !in_array(7,$group_id) ){
                return ['code'=>0,'msg'=>"无发货权限"];
            }
            if($info['status'] == 3){
                return ['code'=>0,'msg'=>"订单已发货"];
            }
            if($info['status'] != 2){
                return ['code'=>0,'msg'=>"不可操作"];
            }
            $info->status= 3;
            $info->send_id= $this->auth->id;
            $info->sendtime = time();
            $info->save();
            return ['code'=>1,'msg'=>"操作成功"];
        }

}
